package com.pinyoumei.app.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by Lanborghini on 2017/12/11.
 */

public class SignatureUtils {
    private static final String TAG = "Log";

    private static final String SIGN_PARAM_NAME = "sign";
    private static String md5;

    /**
     * 创建参数签名.
     * @param parameters
     * 参数键值对.
     * @param secret
     * 签名密钥.
     * @return 签名值.
     */
    public static synchronized String createSignature(final Map<String, Object> parameters, final String secret){
        LogUtils.d(TAG, "createSignature[secret:" + secret + "]...");
        if(parameters == null || parameters.size() == 0) throw new IllegalArgumentException("parameters参数为空!");
        if(secret == null || secret.length() == 0) throw new IllegalArgumentException("签名secret为空!");
        //初始化技术参数集合
        final List<String> params = new ArrayList<>();
        //将参数map数据转换成List<String>集合
        createSignatureParameters(params, parameters);
        //计算参数集合为空
        if(params.size() == 0) throw new IllegalArgumentException("未设置渠道号!");
        //将参数名进行ASCII排序
        Collections.sort(params);
        //字符串拼接 需要先将&字符全部替换掉
        final String source = join(params,"&").replaceAll("&","") + secret;
       LogUtils.d(TAG, "拼接后字符串=>" + source);
        //md5
        md5 = DigestUtils.md5Hex(source);
        return md5;
    }

    //将参数map中的字段以 key1 = value1  key2 = value2 的形式放入一个List的集合中
    private static void createSignatureParameters(final List<String> outParams, final Map<String, ?> parameters){
        if(outParams == null || parameters == null) return;
        //添加参数
        for(Map.Entry<String, ?> entry : parameters.entrySet()){//for循环遍历数组
            //剔除含有令牌名称的参数
            if(SIGN_PARAM_NAME.equalsIgnoreCase(entry.getKey())) continue;
            //参数值
            final Object value = entry.getValue();
            //剔除参数值为NULL的参数
            //if(value == null) continue;//终止本次循环
            //剔除参数值为bool且值为false的参数
            //if((value instanceof Boolean) && !((boolean)value)) continue;
            //剔除参数值为Number且值为0的参数
            //if((value instanceof Number) && ((Number)value).floatValue() == 0) continue;
            //数组类型
            /*if(value instanceof Collection) {
                outParams.add(entry.getKey() + "=" + join((Collection) value, ","));
                continue;
            }
            if(value instanceof Map){
                createSignatureParameters(outParams, (Map) value);
                continue;
            }*/
            String strVal = value.toString();
            /*if(strVal != null && strVal.length() > 0){//如果value的值不为空且value的长度大于0
                com.pinyoumei.app.utils.LogUtils.d(TAG, entry.getKey() + "=" + strVal);
                outParams.add(entry.getKey() + "=" + strVal);
            }*/
            outParams.add(entry.getKey() + "=" + strVal);
        }
    }

    private static String join(final Collection collection, final String sep){
        if(collection == null || collection.size() == 0) return "";
        final StringBuilder builder = new StringBuilder();

        for(Object obj : collection){
            if(obj == null) continue;
            if(builder.length() > 0 && sep != null && sep.length() > 0){
                builder.append(sep);
            }
            builder.append(obj.toString());
        }
        return builder.toString();
    }
}
